/////////////////////////////////////////////////////////////////////////////////
////主场景
/////////////////////////////////////////////////////////////////////////////////
var MainScene = cc.Scene.extend ({
    onEnter:function () {
        this._super();
        this.addChild(new GameMain());
    }
});



/////////////////////////////////////////////////////////////////////////////////
////主面板
/////////////////////////////////////////////////////////////////////////////////
var GameMain = cc.Layer.extend ({
    center: null,

    mNodeRoot: null,

    mMaterialTechniqueContent: ["FiveBalls", "fire", "heart", "flappybird",
    "DiamondRoom", "Beautiful", "MoonSnow", "ALargeGroup",
    "PixelGame", "ThreeLeaves", "Track", "BallAndGrass", "Apple", "TheSea"],
    //"Rose"
    mMaterialTechniquePoint: 0,
    misMoveState: false,

    ctor: function () {
        this._super();
        GameMain.Single = this;
        //sc.Debug.drawVisiableRect(this);
        this.center = sc.World.centerPoint;
        this.initMenu();

        this.mNodeRoot = new cc.Node ();
        this.addChild(this.mNodeRoot);
        this.mNodeRoot.setPosition(this.center);

        this.initShaderSprite();
    },

    initShaderSprite: function () {
        this.mNodeRoot.addChild(new ShaderSprite ("FiveBalls"));
    },

    MoveLeftShaderSprite: function () {
        //if (this.misMoveState ||
        //    this.mMaterialTechniquePoint <= 0)
        //    return;
        //
        //this.mMaterialTechniquePoint--;
        //this.misMoveState = true;
        //this.MoveShaderSpriteBaseDirection (true);
        if (this.misMoveState)
            return;
        this.mMaterialTechniquePoint--;
        if (this.mMaterialTechniquePoint < 0)
            this.mMaterialTechniquePoint = this.mMaterialTechniqueContent.length-1;
        this.misMoveState = true;
        this.MoveShaderSpriteBaseDirection (true);
    },

    MoveRightShaderSprite: function () {
        if (this.misMoveState)
            return;

        this.mMaterialTechniquePoint++;
        if (this.mMaterialTechniquePoint > this.mMaterialTechniqueContent.length-1) {
            this.mMaterialTechniquePoint = 0;
        }
        this.misMoveState = true;
        this.MoveShaderSpriteBaseDirection (false);
    },


    MoveShaderSpriteBaseDirection: function (isleft) {
        var v = isleft ? -1: 1;
        var res = this.mMaterialTechniqueContent [this.mMaterialTechniquePoint];
        var self = this;

        var left = this.mNodeRoot.getChildren() [0];
        var right = new ShaderSprite (res);
        this.mNodeRoot.addChild(right);
        right.setPosition(cc.p (v*960, 0));

        var time = 0.6;
        left.runAction(cc.sequence(
            cc.moveBy(time, cc.p (-960 * v, 0)),
            cc.callFunc (function () {
                left.removeFromParent();
            })
        ));

        right.runAction(cc.sequence (
            cc.moveBy(time, cc.p (-960 * v, 0)),
            cc.callFunc (function () {
                self.misMoveState = false;
            })
        ));
    },


    update: function (dt) {
    },

    onEnterTransitionDidFinish: function () {
        this._super();
    },

    initMenu: function () {
        var distance = 30;

        /// 菜单
        var backButton = new cc.MenuItemImage (
            RES.ui_left,
            RES.ui_left01,
            function ()
            {
                this.MoveLeftShaderSprite ();
            }, this);
        backButton.attr({
            x: sc.World.leftx + distance,
            y: sc.World.downy + distance,
            anchorX: 0,
            anchorY: 0
        });

        var nextButton = new cc.MenuItemImage (
            RES.ui_right,
            RES.ui_right01,
            function ()
            {
                this.MoveRightShaderSprite ();
            }, this);
        nextButton.attr({
            x: sc.World.rightx - distance,
            y: sc.World.downy  + distance,
            anchorX: 1,
            anchorY: 0
        });


        var quitButton = new cc.MenuItemImage (
            RES.ui_quit,
            RES.ui_quit01,
            function ()
            {
                sc.Helper.gameQuit();
            }, this);
        quitButton.attr({
            x: sc.World.leftx + (distance-10),
            y: sc.World.topy  - (distance-10),
            anchorX: 0,
            anchorY: 1
        });

        var menu = new cc.Menu (backButton, nextButton, quitButton);
        menu.setPosition(cc.p(0,0));
        this.addChild(menu,20);
    },

    EMPTY_FUNCTION: function () {
    }
});

GameMain.Single = null;


//---------------------------------------------------
//ShaderSprite
//---------------------------------------------------
var ShaderSprite = cc.Node.extend ({

    ctor: function (technique) {
        this._super ();
        var sprite = new cc.Sprite(RES.bg);
        this.addChild(sprite);

        var mat = this.getMaterial();
        sprite.setGLProgramState(mat.getTechniqueByName(technique).getPassByIndex(0).getGLProgramState());
    },

    getMaterial: function () {
        return cc.Material.createWithFilename(RES.material);
    }

});